#region Microsoft Public License
/*
 * (c) Copyright Chenjun Wu. http://utablesdk.codeplex.com
 * 
 * This source is subject to the Microsoft Public License (Ms-PL).
 * Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 * All other rights reserved.
 */
#endregion

﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UTable.Objects.Controls;

namespace UTable.Objects.Controls
{

    public class UPalette : UControl
    {
        static UPalette()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(UPalette), new FrameworkPropertyMetadata(typeof(UPalette)));
        }

        #region Private Fields

        private Dictionary<Brush, PCube> _colorMap = new Dictionary<Brush, PCube>();
        private Dictionary<PCube, Brush> _colorcubeMap = new Dictionary<PCube, Brush>();

        private Dictionary<int, PCube> _strokeMap = new Dictionary<int, PCube>();
        private Dictionary<PCube, int> _strokecubeMap = new Dictionary<PCube, int>();

        #endregion

        #region Color Cubes

        public PCube c_white;
        public PCube c_black;
        public PCube c_gray;
        public PCube c_red;
        public PCube c_green;
        public PCube c_blue;
        public PCube c_yellow;
        public PCube c_gold;
        public PCube c_pink;
        public PCube c_skyblue;
        public PCube c_purple;
        public PCube c_violet;

        #endregion

        #region Stroke Cubes

        public PCube s_2;
        public PCube s_4;
        public PCube s_6;
        public PCube s_8;

        #endregion

        public UPalette():base()
        {
            currentColor = (new BrushConverter()).ConvertFromString("White") as Brush;
            currentStroke = 2;
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            c_white = this.GetTemplateChild("c_White") as PCube;
            c_black = this.GetTemplateChild("c_Black") as PCube;
            c_gray = this.GetTemplateChild("c_Gray") as PCube;
            c_red = this.GetTemplateChild("c_Red") as PCube;
            c_green = this.GetTemplateChild("c_Green") as PCube;
            c_blue = this.GetTemplateChild("c_Blue") as PCube;
            c_yellow = this.GetTemplateChild("c_Yellow") as PCube;
            c_gold = this.GetTemplateChild("c_Gold") as PCube;
            c_pink = this.GetTemplateChild("c_Pink") as PCube;
            c_skyblue = this.GetTemplateChild("c_SkyBlue") as PCube;
            c_purple = this.GetTemplateChild("c_Purple") as PCube;
            c_violet = this.GetTemplateChild("c_Violet") as PCube;

            s_2 = this.GetTemplateChild("s_2") as PCube;
            s_4 = this.GetTemplateChild("s_4") as PCube;
            s_6 = this.GetTemplateChild("s_6") as PCube;
            s_8 = this.GetTemplateChild("s_8") as PCube;

            c_white.Pressed += OnCubePressed;
            c_black.Pressed += OnCubePressed;
            c_gray.Pressed += OnCubePressed;
            c_red.Pressed += OnCubePressed;
            c_green.Pressed += OnCubePressed;
            c_blue.Pressed += OnCubePressed;
            c_yellow.Pressed += OnCubePressed;
            c_gold.Pressed += OnCubePressed;
            c_pink.Pressed += OnCubePressed;
            c_skyblue.Pressed += OnCubePressed;
            c_purple.Pressed += OnCubePressed;
            c_violet.Pressed += OnCubePressed;

            s_2.Pressed += OnCubePressed;
            s_4.Pressed += OnCubePressed;
            s_6.Pressed += OnCubePressed;
            s_8.Pressed += OnCubePressed;

            _colorMap.Add(c_white.Background, c_white);
            _colorMap.Add(c_black.Background, c_black);
            _colorMap.Add(c_gray.Background, c_gray);
            _colorMap.Add(c_red.Background, c_red);
            _colorMap.Add(c_green.Background, c_green);
            _colorMap.Add(c_blue.Background, c_blue);
            _colorMap.Add(c_yellow.Background, c_yellow);
            _colorMap.Add(c_gold.Background, c_gold);
            _colorMap.Add(c_pink.Background, c_pink);
            _colorMap.Add(c_skyblue.Background, c_skyblue);
            _colorMap.Add(c_purple.Background, c_purple);
            _colorMap.Add(c_violet.Background, c_violet);

            _colorcubeMap.Add(c_white,c_white.Background);
            _colorcubeMap.Add(c_black,c_black.Background);
            _colorcubeMap.Add(c_gray,c_gray.Background);
            _colorcubeMap.Add(c_red,c_red.Background);
            _colorcubeMap.Add(c_green,c_green.Background);
            _colorcubeMap.Add(c_blue,c_blue.Background);
            _colorcubeMap.Add(c_yellow,c_yellow.Background);
            _colorcubeMap.Add(c_gold,c_gold.Background);
            _colorcubeMap.Add(c_pink,c_pink.Background);
            _colorcubeMap.Add(c_skyblue,c_skyblue.Background);
            _colorcubeMap.Add(c_purple,c_purple.Background);
            _colorcubeMap.Add(c_violet,c_violet.Background);

            _strokeMap.Add(2,s_2);
            _strokeMap.Add(4,s_4);
            _strokeMap.Add(6,s_6);
            _strokeMap.Add(8,s_8);

            _strokecubeMap.Add(s_2,2);
            _strokecubeMap.Add(s_4,4);
            _strokecubeMap.Add(s_6,6);
            _strokecubeMap.Add(s_8,8);
        }

        private void OnCubePressed(Object sender, PButtonEventArgs args)
        {
            PCube _cube = sender as PCube;

            if(_cube != null)
            {   
                if(_colorcubeMap.ContainsKey(_cube))
                {
                    Brush old_color = currentColor;
                    PCube old_cube = _colorMap[old_color];
                    if(_cube != old_cube)
                        old_cube.IsChosen = false;

                    currentColor = _colorcubeMap[_cube];
                }
                else if (_strokecubeMap.ContainsKey(_cube))
                {
                    int old_stroke = currentStroke;
                    PCube old_cube = _strokeMap[old_stroke];
                    if (_cube != old_cube)
                        old_cube.IsChosen = false;

                    currentStroke = _strokecubeMap[_cube];
                }
            }
        }

        #region Properties

        public Brush currentColor
        {
            get { return (Brush)GetValue(currentColorProperty); }
            set { SetValue(currentColorProperty, value); }
        }

        public int currentStroke
        {
            get { return (int)GetValue(currentStrokeProperty); }
            set { SetValue(currentStrokeProperty, value); }
        }

        #endregion

        #region Reigster Dependency Property


        public static readonly DependencyProperty currentColorProperty =
                DependencyProperty.Register("currentColor", typeof(Brush), typeof(UPalette));

        public static readonly DependencyProperty currentStrokeProperty =
                DependencyProperty.Register("currentStroke", typeof(int), typeof(UPalette));


        #endregion

    }
}
